<?php
require_once("../system/funciones_bd.php");
require_once("../system/sesion.class.php");
//Conexion base de datos local
$link = mysql_connect("localhost", "root", "");
$sesion = new sesion();
$usuario = $sesion->get("usuario");
if ($usuario == false) {
    header("Location: ../index.php");
} else {
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("sirpof_db1", $link);
    mysql_set_charset('utf8', $link);
    ?>
    <!DOCTYPE html>
    <html lang="de"><!-- use class="debug" here if you develope a template and want to check-->
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
            <!-- some meta tags, important for SEO"--> 
            <meta name="description" content="put a short description in here" />
            <meta name="keywords" content="put your important keywords in here" />
            <meta name="revisit-after" content="7 days" />
            <meta name="robots" content="index,follow" />

            <title>ForensicTooth v1.0</title>


            <link rel="stylesheet" href="../css/inuit.css" />
            <link rel="stylesheet" href="../css/fluid-grid16-1100px.css" />
            <link rel="stylesheet" href="../css/eve-styles.css" />
            <link rel="shortcut icon" href="../icon.png" />
            <link rel="apple-touch-icon-precomposed" href="../img/icon.png" />
            <link rel="stylesheet" href="../css/jquery-ui.css" />
            <script src="../js/jquery-1.9.1.js"></script>
            <script src="../js/jquery-ui.js"></script>            
            <script src="../js/respond-min.js" type="text/javascript"></script>
        </script>
        <script>
            $(function() {
                $("#dialog").dialog({
                    autoOpen: false,
                    modal: true
                });
                $(".abrir")
                        .button()
                        .click(function() {
                            $.ajax({
                                url: "datosPacientecon.php?codigo=" + $(this).attr('codigo') + "&paciente=" + $(this).attr('paciente')
                            }).done(function(result) {
                                $("#dialog #contenido").html(result);
                            });
                            $("#dialog").dialog("open");
                        });
            });
        </script>
        <!--local fallback for JQuery-->

    </head>

    <!--===============================================================  Logo, social and menu =====================================================================================--> 
    <body>
        <div class="wrapper">	
            <a href="../index.php" id="logo"><img src="../img/logo.png" alt="something" />
                <h1 class="accessibility"></h1></a>
            <div class="social">
                <h2>
                <?php echo $sesion->get("usuario"); ?> 
                </h2>
            </div>


            <ul id="nav" class="main">
                <li><a href="#" class="active">Coincidencias</a></li>
                <?php
                $paciente = $_GET["idpaciente"];
                ?>
                <li><a href="odontograma_post.php?idusuario=<?php echo $paciente; ?>">Regresar</a></li>
                <li><a href="../system/cerrarsesion.php">salir</a></li>
            </ul>

        </div><!--end of wrapper div--> 

        <div class="clear"></div>    
        <!--===============================================================  Intro =====================================================================================--> 
        <div class="wrapper">
            <div class="grids">
                <form>
                    <table>
                        <thead>
                            <tr>
                                <th scope="col" >NOMBRE</th>
                                <th scope="col" width="10">VER</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            mysql_select_db("sirpof_db1", $link);
                            $resultTabla = "SELECT id_paciente, nombre, count(*) FROM (SELECT o.* ,p.genero
                                                        FROM odontogramapost o
                                                        INNER JOIN lista l ON o.id_diente = l.idlista
                                                        INNER JOIN paciente_odontograma_post po ON po.id_registro = o.id_registro
                                                        INNER JOIN paciente_postmorten p ON p.id_pacientepost = po.id_paciente) tbl1 
                                                    LEFT JOIN 
                                                        (SELECT DISTINCT concat(p.primer_nombre, ' ', p.segundo_nombre, ' ',p.primer_apellido, ' ', p.segundo_nombre) as nombre, p.genero, o.*, p.id_paciente
                                                        FROM odontograma o 
                                                        inner JOIN lista l on o.id_diente = l.idlista
                                                        INNER JOIN paciente_odontograma po on po.id_registro = o.id_registro
                                                        INNER JOIN paciente p on p.id_paciente = po.id_paciente) tbl2 
                                                    on tbl1.genero = tbl2.genero
                                                    AND tbl1.id_diente = tbl2.id_diente
                                                    AND tbl1.lis_idhallazgo2 = tbl2.lis_idhallazgo2
                                                    AND tbl1.lis_idhallazgo3 = tbl2.lis_idhallazgo3
                                                    AND tbl1.lis_idhallazgo4 = tbl2.lis_idhallazgo4
                                                    AND tbl1.lis_idhallazgo5 = tbl2.lis_idhallazgo5
                                                    AND tbl1.lis_idhallazgo6 = tbl2.lis_idhallazgo6
                                                    GROUP BY nombre ORDER BY 3 DESC LIMIT 3
                                                ";
                            $resultLista = mysql_query($resultTabla, $link);
                            $i = 0;
                            while ($row = mysql_fetch_row($resultLista)) {
                                if ($row[1] == null) {
                                    echo ' ';
                                } else {
                                    echo "<tr>
                            <td>" . $row[1] . "</td>
                            <td><input id='abrir" . $row[0] . "' type='button' class='button abrir' name='seleccion[$i]' codigo='" . $row[0] . "' value='DatosBasicos'></td>
                            </tr>\n";
                                    $i++;
                                }
                            }
                            ?>

                        </tbody>                           
                    </table>

                </form>
                <div id="dialog" title="Datos Basicos">
                    <div id="contenido">
                        CARGANDO....
                    </div>
                    <!-- Fin Contenido -->
                </div>
            </div><!--end of div grids-->

        </div><!--end of div wrapper-->    

        <!--===============================================================  Footer =====================================================================================-->  
        <div class="wrapper">
            <div id="footer">


                <div class="grids">
                    <center><p>ForensicTooth v1.0 </p></center>
                </div><!--end of grids-->
            </div><!--end of footer-->
        </div><!--end of wrapper-->



    </body>
    </html>

    <?php
}
?>
